Skip to main content

Client Side SDK

Installation

Install the SDK via npm:

npm install lancer-client

Getting Started

1. Initialize the Lancer Uploader

Create an instance of the lancer function with your server URL for managing file upload sessions and chunks.

import { lancer } from "lancer-client";

const lancerClient = lancer("<your-server-url>");
ParameterTypeRequiredDescription
serverUrlstringYesURL of your upload server

2. Create an Upload Session

Use the createSession method to start an upload session for a file.

const session = await lancerClient.createSession(file, {
baseChunkSize: 1024 * 1024, // 1MB chunks
authToken: "<your-auth-token>",
provider: "AWS", // Storage provider
});
ParameterTypeRequiredDescription
fileFileYesFile object to be uploaded
baseChunkSizenumberYesSize of each file chunk (bytes)
authTokenstringYesAuthorization token
providerstringYesStorage provider name

Response:

{
"sessionToken": "abc123",
"file": {},
"chunkSize": 1048576,
"max_chunk": 10
}

3. Upload the File

Use the uploadFile method to upload file chunks.

await lancerClient.uploadFile(session, {
onPartUpload: async (data) => {
console.log("Chunk uploaded:", data);
return true; // Continue upload
},
onCompeteUpload: async (data) => {
console.log("Upload completed:", data);
return true;
},
});
ParameterTypeRequiredDescription
sessionobjectYesUpload session object
onPartUploadfunctionYesCallback for each chunk upload
onCompeteUploadfunctionYesCallback for upload completion

Chunk Upload Response:

{
"serverChecksum": "abc123",
"chunk": 1,
"remainingChunk": 9,
"isUploadCompleted": false
}

Final Upload Response:

{
"remainingChunk": 0,
"isUploadCompleted": true,
"uploadId": 456,
"file": {}
}

Error Handling

The SDK throws a LancerUploadStopError if an upload is stopped by a callback returning false.

try {
await lancerClient.uploadFile(session, uploadOptions);
} catch (error) {
if (error instanceof LancerUploadStopError) {
console.error("Upload stopped:", error.message);
}
}

Security Best Practices

  1. Protect Your Auth Token: Store your authToken securely in environment variables.
  2. Verify Checksums: Ensure server verifies chunk checksums.
  3. Limit Upload Size: Set appropriate file size limits on the server.